import { userInfoServer, updateUserInfoServer } from '@/services/user';
import Taro from '@tarojs/taro';

export default {
  namespace: 'user',
  state: {
    // 查询用户信息
    userInfo: undefined,
  },

  effects: {
    // 修改注册用户信息
    *updateUserInfo({ payload }, { call }) {
      const res = yield call(updateUserInfoServer, payload);
      if (res.code !== '1') {
        Taro.showToast({
          title: res.message,
          duration: 1000,
          icon: 'none',
        });
        return Promise.reject();
      }
      return res.content;
    },

    // 查询用户信息
    *getUserInfo({ payload }, { call, put }) {
      yield put({
        type: 'setUserInfo',
        payload: undefined,
      });

      const res = yield call(userInfoServer, payload);
      if (res.code !== '1') {
        Taro.showToast({
          title: res.message,
          duration: 1000,
          icon: 'none',
        });
        return Promise.reject();
      }

      yield put({
        type: 'setUserInfo',
        payload: res.content,
      });

      return res.content;
    },
  },
  reducers: {
    // 查询用户信息
    setUserInfo(state, { payload }) {
      return {
        ...state,
        userInfo: payload,
      };
    },
  },
};
